/*!
 @header SellerServiceViewController.m
 @author 周益
 @version 1.0.0 2013/02/22 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "SellerServiceViewController.h"
#import "Helper.h"
#import "Config.h"
#import "SellerServiceOpeningViewController.h"
#import <QuartzCore/QuartzCore.h>

//  -业务逻辑细节说明-
//
//  @abstract       SellerServiceViewController功能较为简单。根据用户当前的卖家服务类型去判断此view该如何显示,
//                      当用户为“未开通”时，那么此页面应显示“开通普通卖家服务”和“开通担保卖家服务”，并且row可点击；
//                      当用户为“普通卖家”时，那么此页面应显示“升级成为担保卖家”，并且row可点击；
//                      当用户为“担保卖家”时，那么此页面应显示“你已经是担保卖家”，此时row不可点击。

//  @discussion

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本   修改描述

@interface SellerServiceViewController ()

@end

@implementation SellerServiceViewController

@synthesize dismissMode = _dismissMode;

//- (id)initWithType:(NSInteger)type
//{
//    self = [super init];
//    if (self) {
//        _type = type;
//    }
//    return self;
//}

- (id)initWithSeller:(NSDictionary *)seller
{
    self = [super init];
    if (self) {
        _seller = seller;
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = _L(@"vc_title_070");
    self.navigationItem.hidesBackButton = true;
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, _SYS_FRAME_WIDTH - 20, 60)];
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.text = _seller[@"tips"];
    _textView.textColor = _SYS_COLOR_006;
    _textView.font = _SYS_FONT_004;
    _textView.textAlignment = UITextAlignmentCenter;
    _textView.editable = false;
    _textView.layer.cornerRadius = 3;
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = [_SYS_COLOR_003 CGColor];
    [self.view addSubview:_textView];
    

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 80, _SYS_FRAME_WIDTH - 20, 87) style:UITableViewStylePlain];
    _tableView.backgroundView = nil;
    _tableView.dataSource = (id)self;
    _tableView.delegate = (id)self;
    _tableView.scrollEnabled = NO;    //设置不可滚动
    _tableView.layer.borderColor = [_SYS_COLOR_003 CGColor];
    _tableView.layer.cornerRadius = 3;
    _tableView.layer.borderWidth = 1;
    [self.view addSubview:_tableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_PRODUCT_SELLER];
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");
    request.delegate = (id)self;
    request.successSelector = @selector(requestSellerStatisticsSuccess:);
    [request asynchronous];
}

- (void)requestSellerStatisticsSuccess:(NSDictionary *)dict
{
    if ([dict[@"status"] intValue] == 1) {
        
        _seller = dict[@"data"];
        _textView.text = _seller[@"tips"];
    }
    else {
        
        [Helper showAlertWithTitle:dict[@"mesage"] andMessage:nil];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)hasBackButton:(BOOL)flag
{
    //导航栏左按钮：返回
    UIButton* backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)backButtonEvent
{
    if (_dismissMode) {
        
        [self.navigationController dismissModalViewControllerAnimated:true];
    }
    else {
        
        [self.navigationController popViewControllerAnimated:true];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //是未注册卖家显示两行，其他显示一行
//    if ([_seller[@"sellerType"] intValue] == 0) {
        return 2;
//    }
//    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        //卖家服务label
        UILabel *sellerServiceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 12.0f, 200.0f, 20.0f)];
        sellerServiceLabel.font = _SYS_FONT_007;
        sellerServiceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:sellerServiceLabel];
    }
    cell.backgroundColor = [UIColor whiteColor];
    
    //获得卖家服务label
    
    UILabel *sellerServiceLabel = [[cell.contentView subviews] objectAtIndex:0];
    
    //如果用户是未注册卖家
    if ([_seller[@"sellerType"] intValue] == 0) {
        
        switch (indexPath.row) {
            case 0:
                sellerServiceLabel.text = _L(@"title_20");   //开通普通卖家服务
                break;
            case 1:
                
                sellerServiceLabel.text = _L(@"title_21");   //开通担保卖家服务
                break;
                
            default:
                break;
        }
        
    //如果用户是普通卖家
    }else if ([_seller[@"sellerType"] intValue] == 1){
        
        sellerServiceLabel.text = _L(@"title_22");   //升级成为担保卖家
        
    //如果用户是担保卖家
    }else if ([_seller[@"sellerType"] intValue] == 2){
        
        sellerServiceLabel.text = _L(@"title_23");   //你已经是担保卖家
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //如果已经是担保卖家，那么就没有进一步的操作
    if ([_seller[@"sellerType"] intValue] == 2) {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SellerServiceOpeningViewController *sellerServiceOpeningVC = nil;
    
    //如果有申请状态
    if ([_seller[@"applyStatus"] intValue] > 0) {
        
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:_seller[@"tips"] delegate:nil cancelButtonTitle:_L(@"btn_yes_01") otherButtonTitles: nil];
        [alertView show];
        return;
    }
    //如果用户是未注册卖家
    else if ([_seller[@"sellerType"] intValue] == 0) {
    
        switch (indexPath.row) {
            case 0:
            {
                sellerServiceOpeningVC = [[SellerServiceOpeningViewController alloc] initWithType:ServiceApplyNormalType];
                
//                [sellerServiceOpeningVC.navigationItem setTitle:_L(@"title_24")];   //普通卖家申请
            }
                
                break;
            case 1:
            {
                sellerServiceOpeningVC = [[SellerServiceOpeningViewController alloc] initWithType:ServiceApplyVIPType];
                
//                [sellerServiceOpeningVC.navigationItem setTitle:_L(@"title_25")];   //担保卖家申请
            }
                break;
            default:
                break;
        }
        
    //如果用户是普通卖家
    }else if ([_seller[@"sellerType"] intValue] == 1){
        
        sellerServiceOpeningVC = [[SellerServiceOpeningViewController alloc] initWithType:ServiceUpgradeToVIPType];
        
//        [sellerServiceOpeningVC.navigationItem setTitle:_L(@"title_26")];   //担保卖家申请

        
    //如果用户是担保卖家
    }else if ([_seller[@"sellerType"] intValue] == 2){
        
        return;
    }
    
    sellerServiceOpeningVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:sellerServiceOpeningVC animated:YES];
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    if (_dismissMode) {
        [self.navigationController dismissModalViewControllerAnimated:true];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
